package com.second.hand.trading.server.service.impl;

import com.second.hand.trading.server.dao.SalesmanDao;
import com.second.hand.trading.server.model.SalesmanModel;
import com.second.hand.trading.server.service.SalesmanService;
import com.second.hand.trading.server.vo.PageVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
public class SalesmanServiceImpl implements SalesmanService {

    @Resource
    private SalesmanDao salesmanDao;

    /**
     * 查询一个用户的公开信息
     * @param id
     * @return
     */
    public SalesmanModel getSalesman(Long id){
        return salesmanDao.selectByPrimaryKey(id);
    }

    /**
     * 登录，安全问题未解决
     * @param accountNumber
     * @param salesmanPassword
     * @return
     */
    public SalesmanModel salesmanLogin(String accountNumber, String salesmanPassword){
        return salesmanDao.salesmanLogin(accountNumber,salesmanPassword);
    }

    /**
     *注册
     * @param salesmanModel
     * @return
     */
    public boolean salesmanSignIn(SalesmanModel salesmanModel){
        return salesmanDao.insert(salesmanModel) == 1;
    }

    /**
     *修改用户公开信息，未验证用户身份
     * @param salesmanModel
     * @return
     */
    public boolean updateSalesmanInfo(SalesmanModel salesmanModel){
        return salesmanDao.updateByPrimaryKeySelective(salesmanModel)==1;
    }

    /**
     * 修改密码
     * @param newPassword
     * @param oldPassword
     * @param id
     * @return
     */
    public boolean updatePassword(String newPassword, String oldPassword,Long id){
        return salesmanDao.updatePassword(newPassword,oldPassword,id)==1;
    }

    public PageVo<SalesmanModel> getUserByStatus(int status,int page ,int nums){
        List<SalesmanModel> list;
        int count=0;
        if(status==0){
            count=salesmanDao.countNormalSalesman();
            list=salesmanDao.getNormalSalesman((page-1)*nums, nums);
        }else {
            count=salesmanDao.countBanSalesman();
            list=salesmanDao.getBanSalesman((page-1)*nums, nums);
        }
        return new PageVo<>(list,count);
    }

    public PageVo<SalesmanModel> getAllSalesman(int page , int nums) {
        List<SalesmanModel> list;
        int count=0;
        count=salesmanDao.countNormalSalesman()+salesmanDao.countBanSalesman();
        list=salesmanDao.getSalesmanList((page-1)*nums, nums);
        return new PageVo<>(list,count);
    }
}
